require "framework/BaseController"
require "FairyGUI"

CoolDownMainCtrl = BaseController:New({mControllerName="CoolDownMainCtrl"});

function CoolDownMainCtrl:Awake()
	-- body
	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/Cooldown";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("Cooldown", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	self:OnViewCreated(self.mainComponent);

	
	self._time1 = 0;
	self._time2 = 0;
	local tickManager = TickManager.Instance();
	-- tickManager:AddFrameTickListener(self.TickHandler, false);
	fgui.add_timer(1/60.0, 0, self.OnFrameTickHandler, self);
end

function CoolDownMainCtrl:InitView(go)
	-- parse view
	Stage.inst.onKeyDown:Add(CoolDownMainCtrl.OnKeyDown);

	self._btn0 = self.mainComponent:GetChild("b0").asButton;
	self._btn0.icon = "Cooldown/k0";
	self.local_time1 = 5;
	self._mask0 = self._btn0:GetChild("mask").asImage;

	self._btn1 = self.mainComponent:GetChild("b1").asButton;
	self._btn1.icon = "Cooldown/k1";
	self._time2 = 10;
	self._mask1 = self._btn1:GetChild("mask").asImage;

end

function CoolDownMainCtrl:TickHandler(delta)
	print("--------------------------------");
	self._time1 = self._time1 - Time.deltaTime;
	if (self._time1 < 0) then
		self._time1 = 5;
	end
	self._mask0.fillAmount = 1 - (5.0 - self._time1) / 5.0;

	self._time2 = self._time2 - Time.deltaTime;
	if (self._time2 < 0) then
		self._time2 = 10;
	end
	self._btn1.text = Mathf.Round(self._time2);
	self._mask1.fillAmount = 1 - (10 - self._time2) / 10;
end

function CoolDownMainCtrl:OnFrameTickHandler()
	-- body
	self._time1 = self._time1 - Time.deltaTime;
	if (self._time1 < 0) then
		self._time1 = 5;
	end
	self._mask0.fillAmount = 1 - (5.0 - self._time1) / 5.0;

	self._time2 = self._time2 - Time.deltaTime;
	if (self._time2 < 0) then
		self._time2 = 10;
	end
	self._btn1.text = Mathf.Round(self._time2);
	self._mask1.fillAmount = 1 - (10 - self._time2) / 10;
end

function CoolDownMainCtrl:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end